/****************************************************
          File Name:header.h
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Tue 13 Jan 2015 09:33:57 AM CST
*****************************************************/

#ifndef header_h_
#define header_h_

#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/mman.h>
#include<limits.h>

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<stdbool.h>
#include<time.h>

#include<signal.h>
#include<sys/wait.h>

#define ADDR_LEN    (20 * sizeof(char))
#define SA          struct sockaddr
#define CONTENT_SIZE    8192

struct html {
    char *html_page;
    size_t size;
};

typedef struct Rng {
    int offset;                 /* request file start xx - 0 */
    off_t end;                  /* request file size */
} Range;

typedef struct response {
    int confd;                  /* connected sock file descriptor */
    int code;                   /* http response code */
    off_t size;                 /* request file size */
    char *type;                 /* request file type */
    time_t mtime;               /* last modified time */
    Range range;                /* partial content  e206 */
} abby_response;

struct perm {
    uid_t uid;
    gid_t gid;
};

/* permission */
int check(struct perm *pm, const char *name);

void new_user(struct perm *pm, const char *name);

/* html.c */
void make_page(const char *body, struct html *page, bool islist);

void http_respond(abby_response resp);

void parse_ext(char *filetype, const char *path);

/* handle http error */
void pisces_err(int sockfd, int code);

void make_link(int sockfd, const char *rootdir, const char *dir);

/* my_daemon.c */
int my_daemon(void);

/* request_action.c */
void sock_writen(int sockfd, void *filebuf, size_t n);

char *parse_url(const char *url);

void do_cgi(int sockfd, char *path);

#endif
